<template>
  <!-- 歌曲模块 -->
  <div class="mod-song">
    <div class="container">
      <div class="song-hd" v-if="showSong">
        <div class="song-hd-pic">
          <img :src="song[0].al.picUrl" alt="" class="song-pic" />
        </div>
        <div class="song-hd-main">
          <div class="song-title-box">
            <h1 class="song-title">
              <span class="songlist-vip" v-if="song[0].fee == 1">
                <i class="iconfont">&#xec48;</i>
              </span>
              <a href="javascript:;" :title="song[0].name">{{
                song[0].name
              }}</a>
            </h1>
            <span class="song-tns" v-if="song[0].tns">{{
              song[0].tns[0]
            }}</span>
          </div>

          <div class="song-artist-box">
            <h4 class="song-artist">
              歌手：
              <a
                v-for="(art, n) in song[0].ar"
                :key="art.id"
                href="javascript:;"
                :title="art.name"
                @click="clickArtistDetail(art.id)"
              >
                <i v-if="n > 0"> / </i>
                {{ art.name }}
              </a>
            </h4>
          </div>

          <div class="song-al-box">
            <h4 class="song-al">
              所属专辑：
              <a href="javascript:;" @click="clickAlbumDetail(song[0].al.id)">{{
                song[0].al.name
              }}</a>
              <span class="sl-tns" v-if="song[0].al.tns[0]">
                - {{ song[0].al.tns[0] }}</span
              >
            </h4>
          </div>

          <div class="mod-songlist-toolbar">
            <a class="mod-btn-blue">
              <span class="btn-play" @click="clickPlay(song[0])">
                <i class="iconfont">&#xe8a8;</i>
                播放
              </span>
              <span class="btn-add">
                <i class="iconfont" @click="clickAdd(song[0])">&#xe603;</i>
              </span>
            </a>
            <a class="mod-btn" @click="clickCol">
              <i class="iconfont">&#xe8c8;</i>
              收藏
            </a>
            <a class="mod-btn">
              <i class="iconfont">&#xe8a6;</i>
              下载
            </a>
            <a class="mod-btn" @click="clickShare">
              <i class="iconfont">&#xe8af;</i>
              分享
            </a>
            <a class="mod-btn">
              <i class="iconfont">&#xe8a7;</i>
              评论（{{ commentData.total }}）
            </a>
          </div>
        </div>
      </div>
      <div class="song-left" v-if="song.length > 0">
        <div class="song-lyric" v-if="showLyric">
          <!-- <h2 class="lyric-title">歌词</h2> -->
          <!-- :key="item.ms" 有时候key值会报错，是因为歌词毫秒数有些都是0，导致key值重复 -->
          <div :class="['song-lyric-box', { limit: isLimit }]">
            <p
              class="song-lyric-item"
              v-for="(item, i) in lyric[0]"
              :key="i + item.ms"
            >
              {{ item.content }}
            </p>
          </div>
          <p class="song-lyric-item">
            <a class="lyric-height" @click="changeLimit">{{ show }}</a>
          </p>
        </div>
        <div class="song-comment" v-if="commentData">
          <comment ref="comment" :commentData="commentData"></comment>
          <div class="comment-el-pagination" v-if="commentData.total > 20">
            <el-pagination
              background
              @current-change="handleCurrentChange"
              :current-page.sync="currentPage"
              :page-size="20"
              layout="prev, pager, next, jumper"
              :total="commentData.total"
            >
            </el-pagination>
          </div>
        </div>
      </div>
      <div class="song-relative" v-if="song.length > 0">
        <div class="song-relative-box" v-if="song[0].mv !== 0">
          <h2 class="song-relative-hd">mv</h2>
          <mv-list :item="mv" v-if="mv.id"></mv-list>
        </div>
        <div class="song-relative-box" v-if="remv.length > 0">
          <h2 class="song-relative-hd">相关推荐</h2>
          <mlog-list
            :item="item"
            v-for="item in remv"
            :key="item.id"
          ></mlog-list>
        </div>
        <div class="song-relative-box" v-if="resong.length > 0">
          <h2 class="song-relative-hd">相似歌曲</h2>
          <div class="resong-box" v-for="item in resong" :key="item.id">
            <div class="left">
              <a href="javascript:;" @click="clickSong(item.id)">
                <img
                  :src="item.album.picUrl"
                  :alt="item.name"
                  class="resong-pic"
                />
              </a>
            </div>
            <h4 class="resong-name">
              <a
                href="javascript:;"
                :title="item.name"
                @click="clickSong(item.id)"
                >{{ item.name }}</a
              >
            </h4>
            <h4 class="resong-author">
              <a
                v-for="(artist, n) in item.artists"
                :key="artist.id"
                href=""
                :title="artist.name"
              >
                <i v-if="n > 0"> / </i>
                {{ artist.name }}
              </a>
            </h4>
          </div>
        </div>
        <div class="song-relative-box" v-if="replaylist.length > 0">
          <h2 class="song-relative-hd">相似歌单推荐</h2>
          <div class="resong-box" v-for="item in replaylist" :key="item.id">
            <div class="left">
              <a href="javascript:;" @click="clickPlaylistDetail(item.id)">
                <img
                  :src="item.coverImgUrl"
                  :alt="item.name"
                  class="resong-pic"
                />
              </a>
            </div>
            <h4 class="resong-name">
              <a
                href="javascript:;"
                :title="item.name"
                @click="clickPlaylistDetail(item.id)"
                >{{ item.name }}</a
              >
            </h4>
            <h4 class="resong-creator">
              <a href="" :title="item.creator.nickname">
                by
                {{ item.creator.nickname }}
              </a>
            </h4>
          </div>
        </div>
      </div>
    </div>
    <small-tip :tipMessage="smallTipMsg" v-if="showSmallTip"></small-tip>
  </div>
</template>

<script>
import { lrc2Json } from "common/lyric.js";
import Comment from "views/toplists/child/Comment.vue";
import MvList from "components/mvlist/MvList.vue";
import MlogList from "components/mloglist/MlogList.vue";
import SmallTip from "components/tip/SmallTip";

export default {
  components: {
    Comment,
    MvList,
    MlogList,
    SmallTip,
  },
  data() {
    return {
      id: "",
      song: [],
      lyric: [[], []],
      mv: {},
      // 相关mv
      remv: [],
      // 相似歌曲
      resong: [],
      // 相似歌单
      replaylist: [],
      showreplaylist: false,
      currentPage: 1,
      commentData: {},
      isLimit: true,
      show: "[展开]",
      showSong: false,
      showLyric: false,
      showSmallTip: false,
      smallTipMsg: "操作提示",
    };
  },
  mounted() {
    this.id = this.$route.query.id;
    // console.log(this.id);
    this.getSongDetail();
  },
  methods: {
    clickShare() {
      if (this.$store.state.isLogin) {
        let newItem = {
          id: this.song[0].id,
          type: "song",
          name: this.song[0].name,
        };
        this.song[0].ar.forEach((item, index) => {
          if (index == 0) {
            newItem.artist = item.name;
          } else {
            newItem.artist += "/" + item.name;
          }
        });
        console.log(newItem);
        this.$store.commit("changeShowShare", true);
        this.$store.commit("changeShareItem", newItem);
      } else {
        this.smallTipMsg = "请登录后再收藏歌曲";
        this.showSmallTip = true;
        setTimeout(() => {
          this.showSmallTip = false;
        }, 1000);
      }
    },
    clickCol() {
      if (this.$store.state.isLogin) {
        this.$store.commit("changeShowCollect", true);
        this.$store.commit("changeSId", this.song[0].id);
      } else {
        this.smallTipMsg = "请登录后再收藏歌曲";
        this.showSmallTip = true;
        setTimeout(() => {
          this.showSmallTip = false;
        }, 1000);
      }
    },
    clickPlay(item) {
      var newItem = {
        id: item.id,
        name: item.name,
        ar: item.ar,
        al: item.al,
        mv: item.mv,
        dt: item.dt,
      };
      this.playSongs(newItem);
    },
    clickAdd(item) {
      var newItem = {
        id: item.id,
        name: item.name,
        ar: item.ar,
        al: item.al,
        mv: item.mv,
        dt: item.dt,
      };
      this.addSongs(newItem);
    },
    clickArtistDetail(id) {
      this.playArtistDetail(id);
    },
    clickAlbumDetail(id) {
      this.playAlbumDetail(id);
    },
    clickPlaylistDetail(id) {
      this.playPlaylistDetail(id);
    },
    clickSong(id) {
      this.playSong(id);
    },
    changeLimit() {
      this.isLimit = !this.isLimit;
      if (this.isLimit) {
        this.show = "[展开]";
        document.documentElement.scrollTo({
          top: 141,
          behavior: "smooth",
        });
      } else {
        this.show = "[收起]";
      }
    },
    getRelativePlaylist() {
      this.axios({
        /* 相似歌单接口地址 传歌曲id */
        url: "/simi/playlist?id=" + this.id,
      })
        .then((res) => {
          this.replaylist = res.data.playlists;
          this.showreplaylist = true;
          // console.log(this.replaylist);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getRelativeSong() {
      this.axios({
        /* 相似歌曲接口地址 传歌曲id */
        url: "/simi/song?id=" + this.id,
      })
        .then((res) => {
          this.resong = res.data.songs;
          console.log(this.resong);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getRelativeMv() {
      this.axios({
        /* 相关mv接口地址 传歌曲id */
        url: "/mlog/music/rcmd?limit=3&songid=" + this.id,
      })
        .then((res) => {
          this.remv = res.data.data.feeds;
          // console.log(this.remv);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getMvDetail() {
      if (this.song[0].mv !== 0) {
        this.axios({
          /* mv详情接口地址 */
          url: "/mv/detail?mvid=" + this.song[0].mv,
        })
          .then((res) => {
            this.mv = res.data.data;
            // console.log(this.mv);
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    handleCurrentChange() {
      // console.log(this.currentPage);
      // console.log(`当前页: ${val}`);
      this.getComment();
      // 点击分页后，滚动到评论的头部，141为页面头部组件的高度
      document.documentElement.scrollTo({
        top: this.$refs.comment.$el.offsetTop + 141,
        behavior: "smooth",
      });
    },
    getComment() {
      this.axios({
        /* 歌单评论接口地址 */
        url:
          "/comment/music?id=" +
          this.id +
          "&offset=" +
          (this.currentPage - 1) * 20,
      })
        .then((res) => {
          this.commentData = res.data;
          // console.log(this.commentData);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getSongLyric() {
      this.axios({
        /* 歌词接口地址 */
        url: "/lyric?id=" + this.id,
      })
        .then((res) => {
          console.log(res.data);
          // 原歌词
          this.lyric[0] = lrc2Json(res.data.lrc.lyric);
          // 翻译歌词
          // this.lyric[1] = lrc2Json(res.data.tlyric.lyric);
          // console.log(this.lyric);
          this.showLyric = true;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getSongDetail() {
      this.axios({
        /* 歌曲详情接口地址 */
        url: "/song/detail?ids=" + this.id,
      })
        .then((res) => {
          console.log(res);
          this.song = res.data.songs;
          if (this.song.length > 0) {
            this.showSong = true;
          }
          this.getSongLyric();
          this.getComment();
          this.getMvDetail();
          this.getRelativeMv();
          this.getRelativeSong();
          this.getRelativePlaylist();
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
.mod-song {
  padding-top: 50px;
  background-color: var(--color-theme-light-5);
}
.song-hd {
  position: relative;
  height: 250px;
  margin-bottom: 50px;
}
.song-hd-pic {
  position: absolute;
  left: 0;
  top: 0;
  width: 250px;
  height: 250px;
}
.song-pic {
  width: 100%;
  height: 100%;
}
/* song-hd-main */
.song-hd-main {
  position: relative;
  margin-left: 300px;
  height: 250px;
  overflow: hidden;
}
.song-title,
.song-artist,
.song-al {
  font-weight: 400;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.plf-creator-box,
.song-title-box,
.song-artist-box,
.song-al-box {
  height: 50px;
  line-height: 50px;
}
.song-title .songlist-vip .iconfont {
  font-size: 30px;
  color: var(--color-theme);
}

.song-tns {
  position: absolute;
  top: 50px;
  color: #999;
}
.song-artist,
.song-al {
  font-size: 16px;
  margin-bottom: 20px;
}
.song-artist a:hover,
.song-al a:hover {
  color: var(--color-theme);
}
.song-artist {
  margin-top: 20px;
}
.sl-tns {
  color: #999;
  font-size: 14px;
}
/* song-left */
.song-left {
  float: left;
  width: 850px;
}
.song-lyric {
  width: 100%;
  margin-bottom: 50px;
}
.lyric-title {
  font-weight: 400;
  margin-left: 50px;
  height: 50px;
  line-height: 50px;
}
.song-lyric-box.limit {
  max-height: 390px;
  overflow: hidden;
}
.song-lyric-item {
  line-height: 26px;
  font-size: 14px;
  text-align: center;
}
.lyric-height {
  color: var(--color-theme);
  cursor: pointer;
}
.comment-el-pagination {
  position: relative;
  text-align: center;
  margin: 20px auto;
}
/* song-relative */
.song-relative {
  float: right;
  width: 300px;
  overflow: hidden;
}
.song-relative-box {
  position: relative;
  width: 100%;
  overflow: hidden;
}
.song-relative-hd {
  margin-bottom: 10px;
  font-weight: 400;
  height: 36px;
  line-height: 36px;
}
.song-mv-pic {
  width: 300px;
}
.resong-box {
  margin-left: 80px;
  vertical-align: middle;
  height: 70px;
}
.resong-box .left {
  display: inline;
  margin-top: 5px;
  margin-left: -75px;
  overflow: hidden;
}
.resong-box .left a {
  position: relative;
  display: block;
  width: 60px;
  height: 60px;
}
.resong-pic {
  width: 60px;
  height: 60px;
}
.resong-name,
.resong-author,
.resong-creator {
  font-weight: 400;
  padding-top: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.resong-name a,
.resong-author a,
.resong-creator a {
  white-space: nowrap;
}
.resong-name a:hover,
.resong-author a:hover,
.resong-creator a:hover {
  color: var(--color-theme);
}
.mod-song .mod-songlist-toolbar {
  position: absolute;
  bottom: 0;
}
</style>